﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SmartMobile.Data;
using SMARTMobile.Models;
using SMARTMobile.WinPhone;

namespace SMARTMobile.ViewModels.Advance
{
    public class OutletNokiaMerchandisingPotentialViewModel : ViewModelBase
    {
        #region Fields
        private ObservableCollection<OutletNokiaMerchandisingPotentialExtend> _outletNokiaMerchandisingPotentialObservable;
        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<PhonevenforVolumeExtend> _phonevendorVolumeObservable;
        private ObservableCollection<PhoneVendor> _phoneVenderObservable;
        private ObservableCollection<OutletNokiaMerchandisingType> _outletNokiaMerchandisingTypeObservable;
        #endregion

        public OutletNokiaMerchandisingPotentialViewModel()
        {
            this.CreateConnection();
            GetOutletNokiaMerchandisingPotential();
            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };
        }

        #region Properties
        public ObservableCollection<OutletNokiaMerchandisingPotentialExtend> OutletNokiaMerchandisingPotentialObservable
        {
            get { return this._outletNokiaMerchandisingPotentialObservable; }
            set
            {
                this._outletNokiaMerchandisingPotentialObservable = value;
                base.RaisePropertyChanged("OutletNokiaMerchandisingPotentialObservable");
            }
        }

        public ObservableCollection<PhonevenforVolumeExtend> PhonevendorVolumeObservable
        {
            get { return this._phonevendorVolumeObservable; }
            set
            {
                this._phonevendorVolumeObservable = value;
                base.RaisePropertyChanged("PhonevendorVolumeObservable");
            }
        }

        public ObservableCollection<PhoneVendor> PhoneVendorObservable
        {
            get { return this._phoneVenderObservable; }
            set
            {
                this._phoneVenderObservable = value;
                base.RaisePropertyChanged("PhoneVenderObservable");
            }
        }

        public List<InDoorType> InDoorTypeList
        {
            get
            {
                var inDoorList = this._smartMobileContext.InDoorType.ToList();
                return inDoorList;
            }
        }


        public ObservableCollection<OutletNokiaMerchandisingType> OutletNokiaMerchandisingTypeObservable
        {
            get { return this._outletNokiaMerchandisingTypeObservable; }
            set
            {
                this._outletNokiaMerchandisingTypeObservable = value;
                base.RaisePropertyChanged("OutletNokiaMerchandisingTypeObservable");
            }
        }


        #endregion

        #region Command
        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }
        #endregion

        #region Command Action Handlers
        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("InputData");
        }

        private void ExecuteSaveCommand(object commandParams)
        {
            //if (GlobalInformation.SelectedTCARCUS != null)
            //{
            //    this._smartMobileContext.OutletNokiaMerchandisingPotential.DeleteAllOnSubmit(this._smartMobileContext.OutletNokiaMerchandisingPotential.Where(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());

            //    this._smartMobileContext.SubmitChanges();
            //}

            SaveData();

            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }
        #endregion

        #region Method        

        private void GetOutletNokiaMerchandisingPotential()
        {
            this.GetOutletNokiaMerchandisingType();
            this.OutletNokiaMerchandisingPotentialObservable = new ObservableCollection<OutletNokiaMerchandisingPotentialExtend>();
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<OutletNokiaMerchandisingPotential> outletNokiaMerchandisingPotential =
                    this._smartMobileContext.OutletNokiaMerchandisingPotential.Where(
                        p => p.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).OrderBy(a => a.Customerkey).ToList();
                if (outletNokiaMerchandisingPotential.Count > 0)
                {
                    foreach (var item in outletNokiaMerchandisingPotential)
                    {
                        OutletNokiaMerchandisingPotentialExtend outletNokia = new OutletNokiaMerchandisingPotentialExtend
                        {
                            ID_PK = item.ID_PK,
                            Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                            MerchandisingCode = item.MerchandisingCode,
                            MerchandisingName = this.GetMerchandisingName(item.MerchandisingCode),
                            Qty = item.Qty,
                            QtyPicture = item.QtyPicture,
                            WherePicture = item.WherePicture ?? string.Empty
                        };
                        this.OutletNokiaMerchandisingPotentialObservable.Add(outletNokia);
                    }
                }
                else
                {
                    foreach (var item in OutletNokiaMerchandisingTypeObservable)
                    {
                        OutletNokiaMerchandisingPotentialExtend outletNokia = new OutletNokiaMerchandisingPotentialExtend
                        {
                            ID_PK = Guid.NewGuid(),
                            Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                            MerchandisingCode = item.MerchandisingCode,
                            MerchandisingName = GetMerchandisingName(item.MerchandisingCode),
                            QtyPicture = 5,
                            WherePicture = string.Empty 
                        };
                        this.OutletNokiaMerchandisingPotentialObservable.Add(outletNokia);
                    }
       
                    List<OutletNokiaMerchandisingPotential> outletNokiaMerchandisingPotentia = new List<OutletNokiaMerchandisingPotential>();
                    foreach (var item in this.OutletNokiaMerchandisingPotentialObservable)
                    {
                        OutletNokiaMerchandisingPotential merchandising = new OutletNokiaMerchandisingPotential
                        {
                            ID_PK = item.ID_PK,
                            Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                            MerchandisingCode = item.MerchandisingCode,
                            Qty = item.Qty,
                            QtyPicture = item.QtyPicture,
                            WherePicture = item.WherePicture
                        };

                        outletNokiaMerchandisingPotentia.Add(merchandising);
                    }

                    if (OutletNokiaMerchandisingPotentialObservable.Any())
                    {
                        this._smartMobileContext.OutletNokiaMerchandisingPotential.InsertAllOnSubmit(outletNokiaMerchandisingPotentia);
                        this._smartMobileContext.SubmitChanges();
                    }
                }
            }
            else
            {
                foreach (var item in OutletNokiaMerchandisingTypeObservable)
                {
                    OutletNokiaMerchandisingPotentialExtend outletNokia = new OutletNokiaMerchandisingPotentialExtend
                    {
                        ID_PK = Guid.NewGuid(),
                        Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        MerchandisingCode = item.MerchandisingCode,
                        MerchandisingName = GetMerchandisingName(item.MerchandisingCode),
                        Qty = 0,
                        QtyPicture = 1,
                        WherePicture = string.Empty
                    };
                    this.OutletNokiaMerchandisingPotentialObservable.Add(outletNokia);
                   
                }

                if (OutletNokiaMerchandisingPotentialObservable.Any())
                {
                    this._smartMobileContext.OutletNokiaMerchandisingPotential.InsertAllOnSubmit(OutletNokiaMerchandisingPotentialObservable);
                    this._smartMobileContext.SubmitChanges();
                }
            }

            var outletNokiaMerchansing = (from mer in this._smartMobileContext.OutletNokiaMerchandisingPotential
                    where
                        mer.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey
                    select mer).FirstOrDefault();

            if (outletNokiaMerchansing == null)
            {
                List<OutletNokiaMerchandisingPotential> merchandisingList = new List<OutletNokiaMerchandisingPotential>();
                foreach (var item in this.OutletNokiaMerchandisingPotentialObservable)
                {
                    if (GlobalInformation.SelectedTCARCUS != null)
                    {
                        OutletNokiaMerchandisingPotential merchandising = new OutletNokiaMerchandisingPotential
                        {
                            ID_PK = Guid.NewGuid(),
                            Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                            MerchandisingCode = item.MerchandisingCode,
                            PictureURL = item.PictureURL,
                            Qty = item.Qty,
                            QtyPicture = item.QtyPicture,
                            WherePicture = item.WherePicture ?? string.Empty
                        };

                        merchandisingList.Add(merchandising);
                    }
                }

                if (merchandisingList.Any())
                {
                    this._smartMobileContext.OutletNokiaMerchandisingPotential.InsertAllOnSubmit(merchandisingList);
                }

                this._smartMobileContext.SubmitChanges();
            }
        }

        private void GetOutletNokiaMerchandisingType()
        {
            this.OutletNokiaMerchandisingTypeObservable = new ObservableCollection<OutletNokiaMerchandisingType>(this._smartMobileContext.OutletNokiaMerchandisingType.ToList());
        }
        
        private string GetMerchandisingName(string merchandisingCode)
        {
            return this._smartMobileContext.OutletNokiaMerchandisingType.Where(p => p.MerchandisingCode == merchandisingCode).Select(p => p.Description).FirstOrDefault();
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }

        public void UpdateData(string merchanCode, int qty)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                var outletNokiaMerchansing = (from mer in this._smartMobileContext.OutletNokiaMerchandisingPotential
                    where
                        mer.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey &&
                        mer.MerchandisingCode == merchanCode
                    select mer).FirstOrDefault();

                if (outletNokiaMerchansing != null)
                {
                    outletNokiaMerchansing.Qty = qty;
                    this._smartMobileContext.SubmitChanges();
                }
            }
        }

        public void SaveData()
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                this._smartMobileContext.OutletNokiaMerchandisingPotential.DeleteAllOnSubmit(this._smartMobileContext.OutletNokiaMerchandisingPotential.Where(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
                this._smartMobileContext.SubmitChanges();
            }


            List<OutletNokiaMerchandisingPotential> merchandisingList = new List<OutletNokiaMerchandisingPotential>();
            foreach (var item in this.OutletNokiaMerchandisingPotentialObservable)
            {
                if (GlobalInformation.SelectedTCARCUS != null)
                {
                    OutletNokiaMerchandisingPotential merchandising = new OutletNokiaMerchandisingPotential
                    {
                        ID_PK = Guid.NewGuid(),
                        Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        MerchandisingCode = item.MerchandisingCode,
                        PictureURL = item.PictureURL,
                        Qty = item.Qty,
                        QtyPicture = item.QtyPicture,
                        WherePicture = item.WherePicture ?? string.Empty
                    };

                    merchandisingList.Add(merchandising);
                }
            }

            if (merchandisingList.Any())
            {
                this._smartMobileContext.OutletNokiaMerchandisingPotential.InsertAllOnSubmit(merchandisingList);
            }

            this._smartMobileContext.SubmitChanges();
        }

        #endregion
    }
}
